<?php

/**
 * @filename Common.php 
 * @encoding UTF-8 
 * @author yuzhiyuan <307090870@qq.com>
 * @copyright copyright 2013 (c) Emagsoftware Technology Co., Ltd. All Rights reserved.
 * @license ……
 * @datetime 2013-6-22  13:01:24
 * @version 1.0
 * @Description
  */


function p($obj){
    echo '<pre>';
    print_r($obj);
    echo '</pre>';
    
}

//根据分类ID查找父类ID或者名称  $type为0时，找名称，1时找ID
function getCatePar($id=0,$type=0){
    $model = M('Cate');
    $p_id = $model->where("id = ".$id)->getField('p_id');
    $res = $model->where("id = ".$p_id)->find();
    if($type == 0){
        return $res['c_name'];
    }else{
        return $res['id'];
    }
}


/**
 * 字符串截取，支持中文和其他编码
 *
 * 中文只算一个字符
 * 与thinkphp自带的msubstr函数相比，调整了参数了顺序和默认值，并判断被截取后才加上后缀
 * 截断显示字符是不算在长度内的
 *
 * @param string $str
 *            需要转换的字符串
 * @param string $length
 *            截取长度
 * @param string $suffix
 *            截断显示字符 例如...
 * @param string $charset
 *            编码格式
 * @param string $start
 *            开始位置
 * @return string
 */
function msubstr ($str, $length, $suffix = '', $start = 0, $charset = 'utf-8')
{
    if (function_exists("mb_substr")) {
        $slice = mb_substr($str, $start, $length, $charset);
    } elseif (function_exists('iconv_substr')) {
        $slice = iconv_substr($str, $start, $length, $charset);
        if (false === $slice) {
            $slice = '';
        }
    } else {
        $re['utf-8'] = "/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xff][\x80-\xbf]{3}/";
        $re['gb2312'] = "/[\x01-\x7f]|[\xb0-\xf7][\xa0-\xfe]/";
        $re['gbk'] = "/[\x01-\x7f]|[\x81-\xfe][\x40-\xfe]/";
        $re['big5'] = "/[\x01-\x7f]|[\x81-\xfe]([\x40-\x7e]|\xa1-\xfe])/";
        preg_match_all($re[$charset], $str, $match);
        $slice = join('', array_slice($match[0], $start, $length));
    }
    // 如果字符被截取了，加上后缀
    if (strlen($slice) < strlen($str)) {
        $slice .= $suffix;
    }
    return $slice;
}